<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>xiaxuchen的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="SpringBoot中封装集成mybatis（gradle+idea)一、 主要内容 mybatis的初步集成 通用mapper的集成 mybatis分页插件的集成 通用service的集成 整体的封装  二、开始1. mybatis的初步集成项目目录结构  - step 1 在项目的build.gradle的dependencies中添加mybatis依赖，如下（如果gradle版本太低则使用*">
<meta property="og:type" content="article">
<meta property="og:title" content="xiaxuchen的博客">
<meta property="og:url" content="http://yoursite.com/2020/08/07/ssm/ssm%E4%B8%ADdao%E5%B1%82%E6%8A%BD%E5%8F%96(%E4%B8%8D%E5%AE%8C%E6%95%B4%E7%89%88)/index.html">
<meta property="og:site_name" content="xiaxuchen的博客">
<meta property="og:description" content="SpringBoot中封装集成mybatis（gradle+idea)一、 主要内容 mybatis的初步集成 通用mapper的集成 mybatis分页插件的集成 通用service的集成 整体的封装  二、开始1. mybatis的初步集成项目目录结构  - step 1 在项目的build.gradle的dependencies中添加mybatis依赖，如下（如果gradle版本太低则使用*">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20191211005557744.png">
<meta property="og:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20191211004320353.png">
<meta property="og:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20191211011316372.png">
<meta property="article:published_time" content="2020-08-07T15:08:01.958Z">
<meta property="article:modified_time" content="2020-07-19T10:36:53.800Z">
<meta property="article:author" content="xiaxuchen">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20191211005557744.png">
  
    <link rel="alternate" href="./atom.xml" title="xiaxuchen的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="./css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="./" id="logo">xiaxuchen的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="./">Home</a>
        
          <a class="main-nav-link" href="./archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="./atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-ssm/ssm中dao层抽取(不完整版)" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="./2020/08/07/ssm/ssm%E4%B8%ADdao%E5%B1%82%E6%8A%BD%E5%8F%96(%E4%B8%8D%E5%AE%8C%E6%95%B4%E7%89%88)/" class="article-date">
  <time datetime="2020-08-07T15:08:01.958Z" itemprop="datePublished">2020-08-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="SpringBoot中封装集成mybatis（gradle-idea"><a href="#SpringBoot中封装集成mybatis（gradle-idea" class="headerlink" title="SpringBoot中封装集成mybatis（gradle+idea)"></a>SpringBoot中封装集成mybatis（gradle+idea)</h1><h2 id="一、-主要内容"><a href="#一、-主要内容" class="headerlink" title="一、 主要内容"></a>一、 主要内容</h2><ul>
<li>mybatis的初步集成</li>
<li>通用mapper的集成</li>
<li>mybatis分页插件的集成</li>
<li>通用service的集成</li>
<li>整体的封装</li>
</ul>
<h2 id="二、开始"><a href="#二、开始" class="headerlink" title="二、开始"></a>二、开始</h2><h3 id="1-mybatis的初步集成"><a href="#1-mybatis的初步集成" class="headerlink" title="1. mybatis的初步集成"></a>1. mybatis的初步集成</h3><p>项目目录结构</p>
<p><img src="C:\Users\xxc、\AppData\Roaming\Typora\typora-user-images\image-20191211005557744.png" alt="image-20191211005557744"></p>
<pre><code>- step 1 在项目的build.gradle的dependencies中添加mybatis依赖，如下（如果gradle版本太低则使用**compile**也可以）:</code></pre>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; mybatis启动器</span><br><span class="line">implementation(&quot;org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2&quot;)</span><br><span class="line">   implementation(&quot;org.springframework.boot:spring-boot-starter-web:2.1.8.RELEASE&quot;)</span><br><span class="line">   &#x2F;&#x2F; 测试</span><br><span class="line">   implementation(&quot;org.springframework.boot:spring-boot-starter-test:2.1.8.RELEASE&quot;)</span><br><span class="line">   testCompile(&quot;junit:junit:version:4.12&quot;)</span><br><span class="line">   &#x2F;&#x2F; 数据库连接池</span><br><span class="line">   runtime(&quot;mysql:mysql-connector-java:5.1.25&quot;)</span><br></pre></td></tr></table></figure>

<ul>
<li><p>step 2 在springboot的application.properties配置文件中配置如下属性:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">## 数据库连接池属性</span><br><span class="line">spring.datasource.driver-class-name&#x3D; com.mysql.jdbc.Driver</span><br><span class="line">## 下面的根据自己数据库的情况配置</span><br><span class="line">spring.datasource.url&#x3D; jdbc:mysql:&#x2F;&#x2F;localhost:3306&#x2F;exam?useUnicode&#x3D;true&amp;characterEncoding&#x3D;UTF-8&amp;zeroDateTimeBehavior&#x3D;convertToNull&amp;autoReconnect&#x3D;true</span><br><span class="line">spring.datasource.username&#x3D; root</span><br><span class="line">spring.datasource.password&#x3D; root</span><br><span class="line"></span><br><span class="line"># mybatis mapper(也就是dao层)所在的包</span><br><span class="line">mybatis.type-aliases-package&#x3D;com.xxc.test.mapper</span><br><span class="line"># mapper的xml映射文件的位置，一般放在resources文件夹下的mapper文件夹以xxxMapper.xml格式进行命名</span><br><span class="line">mybatis.mapper-locations&#x3D;classpath*:mapper&#x2F;*Mapper.xml</span><br></pre></td></tr></table></figure>
</li>
<li><p>step 3 在application启动类上添加MapperScan注解</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 其中的参数是mapper的包名</span><br><span class="line">@MapperScan(&quot;com.xxc.test.mapper&quot;)</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>step 4 编写实体类</p>
<p><strong>Manager.java</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">package com.xxc.test.entity;</span><br><span class="line"></span><br><span class="line">import java.math.BigInteger;</span><br><span class="line"></span><br><span class="line">public class Manager &#123;</span><br><span class="line"></span><br><span class="line">    private BigInteger id;</span><br><span class="line"></span><br><span class="line">    private String email;</span><br><span class="line"></span><br><span class="line">    private String password;</span><br><span class="line">    </span><br><span class="line">    &#x2F;&#x2F;省略get、set、toString</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>step 5 编写mapper接口以及xml映射文件</p>
<p><strong>ManagerMapper.java</strong></p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">package com.xxc.test.mapper;</span><br><span class="line"></span><br><span class="line">import com.xxc.test.entity.Manager;</span><br><span class="line">import org.springframework.stereotype.Repository;</span><br><span class="line"></span><br><span class="line">import java.util.List;</span><br><span class="line">@Repository</span><br><span class="line">public interface ManagerMapper &#123;</span><br><span class="line"></span><br><span class="line">    List&lt;Manager&gt; selectAll();</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>ManagerMapper.xml</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version&#x3D;&quot;1.0&quot; encoding&#x3D;&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-&#x2F;&#x2F;mybatis.org&#x2F;&#x2F;DTD Mapper 3.0&#x2F;&#x2F;EN&quot;</span><br><span class="line">        &quot;http:&#x2F;&#x2F;mybatis.org&#x2F;dtd&#x2F;mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;!--注意命名空间要与ManagerMapper接口的权限定名相同--&gt;</span><br><span class="line">&lt;mapper namespace&#x3D;&quot;com.xxc.test.mapper.ManagerMapper&quot;&gt;</span><br><span class="line">    &lt;select id&#x3D;&quot;selectAll&quot; resultType&#x3D;&quot;com.xxc.test.entity.Manager&quot; &gt;</span><br><span class="line">        SELECT * FROM manager</span><br><span class="line">    &lt;&#x2F;select&gt;</span><br><span class="line">&lt;&#x2F;mapper&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>step 4 测试</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package com.xxc.test.mapper;</span><br><span class="line"></span><br><span class="line">import org.junit.Test;</span><br><span class="line">import org.junit.runner.RunWith;</span><br><span class="line">import org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line">import org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line">import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@RunWith(SpringJUnit4ClassRunner.class)</span><br><span class="line">@SpringBootTest</span><br><span class="line">public class ManagerMapperTest &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    @Autowired</span><br><span class="line">    private ManagerMapper managerMapper;</span><br><span class="line"></span><br><span class="line">    @Test</span><br><span class="line">    public void testSelectAll()&#123;</span><br><span class="line">        System.out.println(managerMapper.selectAll());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src="C:\Users\xxc、\AppData\Roaming\Typora\typora-user-images\image-20191211004320353.png" alt="image-20191211004320353"></p>
<p>好了，springboot集成mybatis就完成了</p>
<h3 id="2-通用mapper的集成"><a href="#2-通用mapper的集成" class="headerlink" title="2.通用mapper的集成"></a>2.通用mapper的集成</h3><h4 id="1-概述"><a href="#1-概述" class="headerlink" title="1. 概述"></a>1. 概述</h4><p>由于<strong>单表</strong>的增删改查完全是模板代码，手写起来还是比较麻烦，我们可以使用通过mapper来帮我们实现，集成后我们只需要用我们的mapper接口集成CrudMapper接口即可使用，无需编写映射文件</p>
<h4 id="2-集成"><a href="#2-集成" class="headerlink" title="2. 集成"></a>2. 集成</h4><ul>
<li>step 1 在依赖中添加mapper的依赖</li>
</ul>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">implementation(<span class="string">&quot;tk.mybatis:mapper:3.4.0&quot;</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>step 2 配置（<strong>tk.mybatis.spring.mapper.MapperScannerConfigurer</strong>）</li>
</ul>
<p><strong>MyBatisConfig.java</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">package com.xxc.test.config;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;</span><br><span class="line">import org.springframework.boot.autoconfigure.AutoConfigureAfter;</span><br><span class="line">import org.springframework.context.annotation.Bean;</span><br><span class="line">import org.springframework.context.annotation.Configuration;</span><br><span class="line">import tk.mybatis.spring.mapper.MapperScannerConfigurer;</span><br><span class="line"></span><br><span class="line">import java.util.Properties;</span><br><span class="line"></span><br><span class="line">@Configuration</span><br><span class="line">@AutoConfigureAfter(MybatisAutoConfiguration.class)</span><br><span class="line">public class MyBatisConfig&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public MapperScannerConfigurer mapperScannerConfigurer()  &#123;</span><br><span class="line">        MapperScannerConfigurer mapperScannerConfigurer &#x3D; new MapperScannerConfigurer();</span><br><span class="line">        mapperScannerConfigurer.setSqlSessionFactoryBeanName(&quot;sqlSessionFactory&quot;);</span><br><span class="line">        &#x2F;&#x2F; 这里是你的mapper的包名</span><br><span class="line">        mapperScannerConfigurer.setBasePackage(&quot;com.xxc.test.mapper&quot;);</span><br><span class="line">        Properties properties &#x3D; new  Properties();</span><br><span class="line">        properties.setProperty(&quot;notEmpty&quot;, &quot;false&quot;);</span><br><span class="line">        properties.setProperty(&quot;IDENTITY&quot;, &quot;MYSQL&quot;);</span><br><span class="line">        properties.setProperty(&quot;ORDER&quot;, &quot;BEFORE&quot;);</span><br><span class="line">        mapperScannerConfigurer.setProperties(properties);</span><br><span class="line">        return mapperScannerConfigurer;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li><p>step 3 修改实体类</p>
<p><strong>Manager2.java</strong></p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.xxc.test.entity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.persistence.*;</span><br><span class="line"><span class="keyword">import</span> java.math.BigInteger;</span><br><span class="line"><span class="keyword">import</span> java.sql.Timestamp;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Table(name = &quot;manager&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Manager2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 一定要给注解加上Id注解进行标识</span></span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="comment">// 主键生成策略</span></span><br><span class="line">    <span class="meta">@GeneratedValue(generator = &quot;JDBC&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> BigInteger id;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String email;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String password;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//当属性名与类名不同时使用该注解关联</span></span><br><span class="line">    <span class="meta">@Column(name = &quot;gmt_create&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> Timestamp gmtCreate;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 当属性没有与数据库的对应关系时，则使用该注解使其忽略</span></span><br><span class="line">    <span class="meta">@Transient</span></span><br><span class="line">    <span class="keyword">private</span> Manager useless;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li><p>step 4 修改mapper接口</p>
<p><strong>ManagerMapper2.java</strong></p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.xxc.test.mapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.xxc.test.entity.Manager2;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Repository;</span><br><span class="line"><span class="keyword">import</span> tk.mybatis.mapper.common.base.select.SelectAllMapper;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ManagerMapper2</span> <span class="keyword">extends</span> <span class="title">SelectAllMapper</span>&lt;<span class="title">Manager2</span>&gt; </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<ul>
<li>step 5 测试</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.xxc.test.mapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@RunWith(SpringJUnit4ClassRunner.class)</span></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ManagerMapper2Test</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ManagerMapper2 managerMapper2;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectAll</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(managerMapper2.selectAll());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src="C:\Users\xxc、\AppData\Roaming\Typora\typora-user-images\image-20191211011316372.png" alt="image-20191211011316372"></p>
<p>通用mapper到此就结束了？还没，虽然mapper提供了很多功能实现的接口给我们，但是一个个继承有些麻烦，我们可以封装一个CrudMapper接口，使得我们直接继承该接口就可以有增删改查的方法</p>
<h4 id="CrudMapper接口封装"><a href="#CrudMapper接口封装" class="headerlink" title="CrudMapper接口封装"></a>CrudMapper接口封装</h4><p>代码如下:</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/08/07/ssm/ssm%E4%B8%ADdao%E5%B1%82%E6%8A%BD%E5%8F%96(%E4%B8%8D%E5%AE%8C%E6%95%B4%E7%89%88)/" data-id="ckdkdagtw0007vout9rw8fk1j" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="./2020/08/07/%E5%B7%A5%E5%85%B7/%E5%90%88%E5%B9%B6%E5%88%86%E6%9E%9D%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="./2020/08/07/ssm/springmvc%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="./archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E9%93%BE%E8%A1%A8/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%95%B0%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/SpringBoot%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 xiaxuchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="./" class="mobile-nav-link">Home</a>
  
    <a href="./archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="./fancybox/jquery.fancybox.css">

  
<script src="./fancybox/jquery.fancybox.pack.js"></script>




<script src="./js/script.js"></script>




  </div>
</body>
</html>